﻿using System.Globalization;
using IveelySE.NLP.Segment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace IveelySE.NLP.Recognition
{
    /// <summary>
    /// 地理名称识别
    /// </summary>
    [Serializable]
    public class WordAnalysis : HMM
    {
        private static WordAnalysis wordAnalysis;

        private string[] delimeter = new string[] { " ", "-", "。", "，", "!" };

        private string[] states = new string[]
                                      {
                                          "ag", //形语词 
                                          "a",  //形容词
                                          "ad", //副形词
                                          "an", //名形词
                                          "Bg", //区别语素
                                          "b",  //区别词
                                          "c",  //连词
                                          "Dg", //副语素
                                          "d",  //副词
                                          "e",  //叹词
                                          "f",  //方位词
                                          "g",  //语素
                                          "h",  //前接成分
                                          "i",  //成语
                                          "j",  //简略语
                                          "k",  //后接成分
                                          "l",  //习用语
                                          "Mg", //数语素
                                          "m",  //数词
                                          "Ng", //名语素
                                          "n",  //名词
                                          "nr", //人名
                                          "ns", //地名
                                          "nt", //机构团体
                                          "nx", //外文字符
                                          "nz", //其它专名
                                          "o",  //拟声词
                                          "p",  //介词
                                          "Qg", //量语素
                                          "q",  //量词
                                          "Rg", //代语素
                                          "r",  //代词
                                          "s",  //处所词
                                          "Tg", //时间语素
                                          "t",  //时间词
                                          "Ug", //助语素
                                          "u",  //助词
                                          "Vg", //动语素
                                          "v",  //动词
                                          "vd", //副动词
                                          "vn", //名动词
                                          "w",  //标点符号
                                          "x",  //非语素字
                                          "Yg", //语气语素
                                          "y",  //语气词
                                          "z"   //状态词
                                      };

        //private  string[] states = new string[]
        //                              {

        //                                  "nr", //人名
        //                                  "ns", //地名
        //                                  "nt", //机构团体

        //                                  "nz", //其它专名

        //                                  "t",  //时间词
        //                                  "x"

        //                              };


        private WordAnalysis()
        {
            this.SetState(this.states);
            this.Train(File.ReadAllLines(@"Resource\199801.txt", Encoding.UTF8));
        }

        private void Train(string[] lines)
        {

            string lastwordType = states[0];
            foreach (string line in lines)
            {
                if (!line.Contains(" "))
                {
                    continue;

                }
                string sentence = line.Substring(line.IndexOf(' '), line.Length - line.IndexOf(' ')).Trim().Replace("[", " ").Replace("]", " ");
                string[] terms = sentence.Split(this.delimeter, StringSplitOptions.RemoveEmptyEntries);
                foreach (string term in terms)
                {
                    string[] words = term.Split('/');
                    if (words.Count() >= 2)
                    {
                        this.AddObserver(words[0]);

                        string currentWordType = words[1];
                        if (!string.IsNullOrWhiteSpace(currentWordType))
                        {
                            AddInitialStateProbability(currentWordType, 1);
                            AddComplexProbability(currentWordType, words[0]);
                            AddTransferProbability(lastwordType, currentWordType, 1);
                            lastwordType = currentWordType;
                        }
                    }
                }

            }
        }



        public string GetWordType(string text)
        {
            Segment.Participle participle = Participle.GetInstance();
            string splitResult = participle.Split(text);
            string[] words = splitResult.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            //words = "/我/说/今天/天气/真好/".Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            double pro;
            int[] path = this.Decode(words, out pro, 0);
            string result = string.Empty;
            for (int i = 0; i < words.Length; i++)
            {
                result += words[i] + "/" + states[path[i]]+" ";
            }
            return result;
        }

        public static WordAnalysis GetInstance()
        {
            if (wordAnalysis == null && File.Exists("wordAnalysis.Ser"))
            {
                wordAnalysis = (WordAnalysis)IveelySE.Common.Serialize.UnSerailzable("wordAnalysis.Ser");
            }
            else if (wordAnalysis == null)
            {
                wordAnalysis = new WordAnalysis();
                IveelySE.Common.Serialize.Serailzable(wordAnalysis, "wordAnalysis.Ser");
            }
            //TODO:在语料库中添加
            for(int i = 0; i < 5000; i++)
            {

               wordAnalysis. AddInitialStateProbability("t", 1);
               wordAnalysis. AddComplexProbability("t", i+"年");
            }
            return wordAnalysis;
        }

        #region Test

       

        #endregion
    }
}
